#! /usr/bin/env python3
# -*-coding: utf-8-*-


__author__ = "Moonkie"


import numpy as np
import cv2
import mxnet as mx


def svd(data):
    '''奇异值分解函数

    奇异值分解函数实现
    '''
    # rx,ry = data.shape[:2]
    # u,sigma,v = np.linalg.svd(data,full_matrices=False)
    # # x,y = sigma.shape
    # h = int(rx * .1)
    # sim = sigma[:h]
    # # sig = np.ndarray([h,ry])
    # # for i in range(h):
    # #     sig[i] = np.diag(sim[i])
    # sig = np.diag(sim,0)
    # u1 = np.zeros((rx,h),float)
    # u1[:,:] = u[:,:h]
    # v1 = np.zeros((h,ry),float)
    # v1[:,:] = v[:h,:]
    # img = u1.dot(sig).dot(v1)
    # a,b,c,d = img.shape
    # # img = img.reshape(a,b*c,d)
    # img.dtype = 'uint8'
    # print(data.dtype)
    # print(img)
    
    # print(data)
    # u, sigma, vt = np.linalg.svd(data)  
    # h1 = int(h * .1) #取前10%的奇异值重构图像  
    # sigma1 = np.diag(sigma[:h1],0)  #用奇异值生成对角矩阵  
    # u1 = np.zeros((h,h1,dim), float)  
    # u1[:,:] = u[:,:h1,:]  
    # vt1 = np.zeros((h1,w,dim), float)  
    # vt1[:,:] = vt[:h1,:,:]  
    # img = u1.dot(sigma1).dot(vt1)
    # img.dtype='uint8'
    # print(img)
    

    # sigvects = np.dot(u*np.diag(sigma),v)
    # img = np.dot(u,sig)
    # print(sig.shape)
    # cv2.imshow('test',img)
    # cv2.waitKey(0)
    # cv2.destroyAllWindows()
    # return sigma,sigvects


if __name__ == "__main__":
    # debug test
    # t = np.mat("4 11 14;8 7 -2")
    # sigma,sinvects = svd(t)
    # print(sinvects)
    # end debug test

    # test image
    img = cv2.imread('timg.jpg',cv2.IMREAD_GRAYSCALE)
    # img = cv2.imread('timg.jpg')
    # cv2.imshow('test',img)
    # cv2.waitKey(0)
    # cv2.destroyAllWindows()
    sigma,img = svd(img)

    # end test
    